package com.company.project.command.supplier;

import com.company.project.exception.NormalException;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @Author dawn
 * Date on 2020/11/26  9:27
 */
@Data
public class SupplierCreateOrUpdateCommand {

    /**
     * 供应商名称
     */
    @NotNull(message = "供应商名称不能为空")
    private String name;

    /**
     * 所属方ID
     */
    private String owner;

    /**
     * 编码
     */
    private String code;
    /**
     * 联系地址
     */
    @NotNull(message = "联系地址不能为空")
    private String address;
    /**
     * 联系人
     */
    @NotNull(message = "联系人不能为空")
    private String contact;
    /**
     * 电话
     */
    @NotNull(message = "联系电话不能为空")
    private String phone;

    /**
     * 结算银行
     */
    private String bankName;

    /**
     * 开户行
     */
    private String accountBankName;

    /**
     * 结算银行帐号
     */
    private String bankAccountNo;

    /**
     * 创建人
     */
    private String createdBy;


    @CreatedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @LastModifiedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date lastModifiedDate;

    public void validData(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (ObjectError oe : bindingResult.getAllErrors()) {
                stringBuilder.append(oe.getDefaultMessage() + "\n");
            }
            throw new NormalException(stringBuilder.toString());
        }
    }
}
